package system;

import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * class Controller
 * 
 * @author Rezan Achmad
 */
public abstract class Controller extends HttpServlet {

    protected Boolean isPost = false;
    protected Boolean isGet = true;
    protected HashMap<String, Boolean> rules = new HashMap<String, Boolean>();
    protected String servletPath;
    protected UserIdentity identity;

    private void preProcessRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Tempe.request = Model.request = request;
        setServletPath(request);
        initUserIdentity(request);
        initRules();
        if (processRules(request, response)) {
            processRequest(request, response);
        }
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

    protected void setView(String userPath, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String url = "/WEB-INF/views" + userPath + ".jsp";
        request.getRequestDispatcher(url).forward(request, response);
    }

    protected void setErrorView(HttpServletRequest request, HttpServletResponse response, int code, String message) throws IOException, ServletException {
        request.setAttribute("code", code);
        request.setAttribute("message", message);
        setView("/site/error", request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        isGet = true;
        isPost = false;
        preProcessRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        isGet = false;
        isPost = true;
        preProcessRequest(request, response);
    }

    protected void setServletPath(HttpServletRequest request) {
        servletPath = request.getServletPath();
    }

    protected abstract void initRules();

    protected void initUserIdentity(HttpServletRequest request) {
        identity = new UserIdentity(request);
    }

    protected boolean processRules(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Boolean value = rules.get(servletPath);

        if (value != null && value == false) {
            setErrorView(request, response, 403, "Anda tidak memiliki otoritas untuk mengakses halaman ini.");
            return false;
        }

        return true;
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}